﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BitmapTagger;
using System.IO;
using System.Windows.Media.Imaging;

namespace TagPrinter
{
	class Program
	{
		[STAThread]
		static void Main(string[] args)
		{
			
			ArgumentParser argumentParser = new ArgumentParser(args);

#if DEBUG
			PrintArgumentParserOptions(argumentParser);
			Console.WriteLine();
#endif

			if (argumentParser.HelpAsked)
			{
				Console.WriteLine(argumentParser.HelpString);
				Console.WriteLine("Press any key to exit");
				Console.ReadKey();
				return;
			}

			if (!argumentParser.HasFilePaths)
			{
				Console.WriteLine("Please pass in at least one file path in the arguments.");
				Console.WriteLine("Press any key to exit");
				Console.ReadKey();
				return;
			}

			foreach (string filepath in argumentParser.FilePaths)
			{
				string imagePath = filepath;

				//make sure it is a png or jpg
				string extension = System.IO.Path.GetExtension(imagePath);
				bool isJPEG = extension.Equals(".jpg");
				bool isPNG = extension.Equals(".png");
				if (!isJPEG && !isPNG)
				{
					Console.WriteLine("Only jpegs and pngs can be read.");
					Console.WriteLine(imagePath + " is not a jpeg or png");
				}
				else
				{

					try
					{
						var tagger = BitmapTagger.BitmapTagger.Create(imagePath, 
							argumentParser.LoadExif, argumentParser.LoadXMPExif, 
							argumentParser.LoadXMP, argumentParser.Precedence);
						PrintImageTagger(tagger);
					}
					catch (Exception)
					{
						Console.WriteLine("Sorry, we are unable to read the image: {0}", imagePath);
						Console.WriteLine("It is possible the image is corrupted or that we just don't support it at this time.");
					}

					

					Console.WriteLine();
				}

			}
			Console.WriteLine();
			Console.WriteLine("Press any key to exit");
			Console.ReadKey();
			
		}

		/// <summary>
		/// Print out all the argument parsers parsed options
		/// </summary>
		/// <param name="parser">The argument parser to print</param>
		static void PrintArgumentParserOptions(ArgumentParser parser)
		{
			Console.WriteLine("LoadExif: {0}", parser.LoadExif);
			Console.WriteLine("LoadXMPExif: {0}", parser.LoadXMPExif);
			Console.WriteLine("LoadXMP: {0}", parser.LoadXMP);

			string precedence = "";
			if (parser.Precedence == TagPrecedence.Exif_XMP_XMPExif)
				precedence = "Exif > XMP > XMPExif";
			else if (parser.Precedence == TagPrecedence.Exif_XMPExif_XMP)
				precedence = "Exif > XMPExif > XMP";
			else if (parser.Precedence == TagPrecedence.XMP_Exif_XMPExif)
				precedence = "XMP > Exif > XMPExif";
			else if (parser.Precedence == TagPrecedence.XMP_XMPExif_Exif)
				precedence = "XMP > XMPExif > Exif";
			else if (parser.Precedence == TagPrecedence.XMPExif_Exif_XMP)
				precedence = "XMPExif > Exif > XMP";
			else if (parser.Precedence == TagPrecedence.XMPExif_XMP_Exif)
				precedence = "XMPExif > XMP > Exif";

			Console.WriteLine("Tag Precedence: {0}", precedence);

			Console.WriteLine("Help Asked: {0}", parser.HelpAsked);

			Console.WriteLine("The following files where passed:");
			foreach (string file in parser.FilePaths)
			{
				Console.WriteLine("   {0}", file);
			}
		}

		/// <summary>
		/// Print out all the tags for an image tagger
		/// </summary>
		/// <param name="tagger">The image tagger to print</param>
		static void PrintImageTagger(BitmapTagger.BitmapTagger tagger)
		{
			Console.WriteLine("Reading Image: {0}", tagger.FilePath);
			Console.WriteLine("Image Width: {0}", tagger.ImageWidth);
			Console.WriteLine("Image Height: {0}", tagger.ImageHeight);

			if (tagger.YResolution != null)
				Console.WriteLine("Y Resolution: {0}", tagger.YResolution);

			if (tagger.XResolution != null)
				Console.WriteLine("X Resolution: {0}", tagger.XResolution);

			if (tagger.ResolutionUnit != null)
				Console.WriteLine("Resolution Unit: {0}", tagger.ResolutionUnit);

			if (tagger.DateTime != null)
				Console.WriteLine("DateTime: {0}", tagger.DateTime);


			if (tagger.ImageDescription != null)
				Console.WriteLine("Image Description: {0}", tagger.ImageDescription);

			if (tagger.Make != null)
				Console.WriteLine("Make: {0}", tagger.Make);

			if (tagger.Model != null)
				Console.WriteLine("Model: {0}", tagger.Model);

			if (tagger.Software != null)
				Console.WriteLine("Software: {0}", tagger.Software);

			if (tagger.Artist != null)
				Console.WriteLine("Artist: {0}", tagger.Artist);

			if (tagger.Copyright != null)
				Console.WriteLine("Copyright: {0}", tagger.Copyright);

			if (tagger.UserComment != null)
				Console.WriteLine("UserComment: {0}", tagger.UserComment);

			if (tagger.DateTimeOriginal != null)
				Console.WriteLine("DateTimeOriginal: {0}", tagger.DateTimeOriginal);

			if (tagger.DateTimeDigitized != null)
				Console.WriteLine("DateTimeDigitized: {0}", tagger.DateTimeDigitized);

			if (tagger.MeteringMode != null)
				Console.WriteLine("Metering Mode: {0}", tagger.MeteringMode);

			if (tagger.MakerNote != null)
				Console.WriteLine("Manufacturer Note: {0}", tagger.MakerNote);

			if (tagger.SensingMethod != null)
				Console.WriteLine("Sensing Method: {0}", tagger.SensingMethod);

			if (tagger.ExposureProgram != null)
				Console.WriteLine("Exposure Program: {0}", tagger.ExposureProgram);

			if (tagger.ISOSpeedRatings != null)
				Console.WriteLine("ISO Speed Ratings: {0}", tagger.ISOSpeedRatings);

			if (tagger.LightSource != null)
				Console.WriteLine("Light Source: {0}", tagger.LightSource);

			if (tagger.ExposureTime != null)
				Console.WriteLine("Exposure Time: {0}", tagger.ExposureTime);

			if (tagger.FNumber != null)
				Console.WriteLine("FNumber: {0}", tagger.FNumber);

			if (tagger.Aperture != null)
				Console.WriteLine("Aperture: {0}", tagger.Aperture);

			if (tagger.ShutterSpeed != null)
				Console.WriteLine("Shutter speed: {0}", tagger.ShutterSpeed);

			if (tagger.UnitHeight != null)
				Console.WriteLine("Height in resolution units: {0}", tagger.UnitHeight);

			if (tagger.UnitWidth != null)
				Console.WriteLine("Width in resolution units: {0}", tagger.UnitWidth);
		}

	}
}
